﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using FileDB;

namespace Rem.Objects.Forms
{
    public partial class UcDataGrid : BaseUserControl
    {
        public UcDataGrid()
        {
            InitializeComponent();
        }

        public BaseObjectTypes ObjectType { get; set; }
        public UcDataGrid(Object dataSource, Dictionary<string, string> fieldNameHeadTextMapping, List<string> invisiableFieldNames)
            : this()
        {
            InitializateGridView(this.dataGridView1, dataSource, fieldNameHeadTextMapping, invisiableFieldNames);
        }

        public void InitializateGridView(Object dataSource,
           Dictionary<string, string> fieldNameHeadTextMapping,
           List<string> invisiableFieldNames)
        {
            this.Data = dataSource;

            InitializateGridView(this.dataGridView1, dataSource, fieldNameHeadTextMapping, invisiableFieldNames);
        }

        public static void InitializateGridView(DataGridView dataGridView, Object dataSource,
            Dictionary<string, string> fieldNameHeadTextMapping,
            List<string> invisiableFieldNames)
        {
            if (dataSource == null)
            {
                return;
            }
            dataGridView.DataSource = dataSource;
            dataGridView.AllowUserToResizeColumns = true;
            dataGridView.AllowUserToOrderColumns = true;
            dataGridView.AllowUserToResizeRows = true;
            
            if (fieldNameHeadTextMapping != null)
            {
                foreach (KeyValuePair<string, string> fieldNameHeadText in fieldNameHeadTextMapping)
                {
                    if (dataGridView.Columns.Contains(fieldNameHeadText.Key))
                    {
                        dataGridView.Columns[fieldNameHeadText.Key].SortMode = DataGridViewColumnSortMode.Automatic;
                        dataGridView.Columns[fieldNameHeadText.Key].HeaderText = fieldNameHeadText.Value;
                        dataGridView.Columns[fieldNameHeadText.Key].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                        dataGridView.Columns[fieldNameHeadText.Key].Resizable = DataGridViewTriState.True;
                        
                    }
                }
            }
            if (invisiableFieldNames != null)
            {
                foreach (string invisiableFieldName in invisiableFieldNames)
                {
                    if (dataGridView.Columns.Contains(invisiableFieldName))
                    {
                        dataGridView.Columns[invisiableFieldName].Visible = false;
                    }
                }
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                Object data = dataGridView1.SelectedRows[0].DataBoundItem;
                if (data != null)
                {
                    FormHelper.OpenObjectForm(this.ObjectType, data);
                }
            }
        }

        private void refreshToolStripButton_Click(object sender, EventArgs e)
        {

        }

        internal bool DeleteSelected()
        {
            bool result = false;
            try
            {
                for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
                {
                    Object data = dataGridView1.SelectedRows[i].DataBoundItem;
                    if (data != null)
                    {
                        switch (this.ObjectType)
                        {
                            case BaseObjectTypes.EmployeeQuery:
                                (data as Employee).Delete();
                                break;
                            case BaseObjectTypes.ContractQuery:
                                (data as Contract).Delete();
                                break;
                            case BaseObjectTypes.CustomerQuery:
                                (data as Customer).Delete();
                                break;
                            case BaseObjectTypes.ReminderQuery:
                                (data as Reminder).Delete();
                                break;
                            default:
                                break;
                        }
                        result = true;
                    }
                }
            }
            catch (Exception)
            {
                result = false;
                MessageBox.Show("删除数据出错，可能数据已不存在或被关联！");
            }
           
            return result;
        }
    }
}
